﻿using System;
using System.Windows.Forms;
using System.Globalization;

namespace DataUpload
{
    public partial class UCBaseFormat : UserControl
    {
        private string _filePath;
        private bool _isChanged;
        private Form _container;
        internal CultureInfo _cultureInfo;

        public bool IsChanged
        {
            get { return _isChanged; }
            set { _isChanged = value; }
        }

        public bool IsValid
        {
            get { return Check(); }
        }

        public UCBaseFormat()
        {
            InitializeComponent();
        }

        public UCBaseFormat(string filePath, Form container)
        {
            InitializeComponent();
            _filePath = filePath;
            _isChanged = true;
            _container = container;
            LoadDefaultValues(_filePath);
        }

        private void LoadDefaultValues(string filePath)
        {
            string ext = filePath.Substring(filePath.LastIndexOf("."));
            switch (ext)
            {
                case ".csv":
                    txtRowSep.Text = "\\r\\n";
                    txtRowSep.ReadOnly = true;
                    txtColSep.Text = ",";
                    txtColSep.ReadOnly = true;
                    txtQuote.Text = "\"";
                    txtQuote.ReadOnly = true;
                    break;
                case ".xls":
                case ".mdb":
                case ".mdf":
                case ".xlsx":
                    txtColSep.Text = "default";
                    txtColSep.ReadOnly = true;
                    txtRowSep.Text = "default";
                    txtRowSep.ReadOnly = true;
                    txtTableSep.Text = "default";
                    txtTableSep.ReadOnly = true;
                    txtQuote.Text = "default";
                    txtQuote.ReadOnly = true;
                    ckcHasExplicitTable.Checked = false;
                    ckcHasExplicitTable.Enabled = false;
                    break;
            }
        }

        private void allControls_TextChanged(object sender, EventArgs e)
        {
            _isChanged = true;
        }

        private void lnkMoreCulture_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            FrmCultureInfo frmCultureInfo = ((FrmBaseFormat)_container).frmCultureInfo;
            if (frmCultureInfo == null)
                frmCultureInfo = new FrmCultureInfo();

            if (frmCultureInfo.ShowDialog() == DialogResult.OK)
            {
                string name = frmCultureInfo.txtCultureName.Text;
                if (!cboCulture.Items.Contains(name))
                {
                    cboCulture.Items.Add(name);
                    cboCulture.SelectedIndex = cboCulture.Items.Count - 1;
                }
                else
                {
                    cboCulture.Text = name;
                }
            }
        }

        private void cboCulture_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboCulture.SelectedIndex > -1)
            {
                if (_cultureInfo == null || _cultureInfo.Name != cboCulture.Text)
                {
                    _cultureInfo = new CultureInfo(cboCulture.Text, false);
                    txtDecimal.Text = _cultureInfo.NumberFormat.NumberDecimalSeparator;
                    txtDigit.Text = _cultureInfo.NumberFormat.NumberGroupSeparator;
                    txtDateFormat.Text = _cultureInfo.DateTimeFormat.ShortDatePattern;
                    txtTimeFormat.Text = _cultureInfo.DateTimeFormat.LongTimePattern;
                }
            }
        }

        private bool Check()
        {
            if (txtColSep.Text == ""
                || txtRowSep.Text == ""
                || txtQuote.Text == ""
                || txtDecimal.Text == ""
                || txtDigit.Text == ""
                || txtDateFormat.Text == ""
                || txtTimeFormat.Text == ""
                || cboCulture.Text == "")
                return false;

            try
            {
                _cultureInfo = new CultureInfo(cboCulture.Text, false);
            }
            catch (Exception exc)
            {
                DBConnection.MyDebugClass.LogThis("Failed to construct new culture info named " + cboCulture.Text, exc);
                return false;
            }
            return true;
        }
    }
}
